﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SentimentAnalysis
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            happyadder();
            textBox1.Text = "";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            neutadder();
            textBox1.Text = "";
        }
        private void button3_Click(object sender, EventArgs e)
        {
            sadadder();
            textBox1.Text = "";
        }
        public void happyadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("happysent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("happywords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("happywords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        public void sadadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("sadsent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {
                StreamReader file2 = new StreamReader("sadwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("sadwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        public void neutadder()
        {
            string[] x = textBox1.Text.Split(' ');
            StreamWriter xer = new StreamWriter("neutsent.txt", true);
            xer.WriteLine(textBox1.Text);
            xer.Close();
            int yx = 0;
            foreach (string i in x)
            {

                StreamReader file2 = new StreamReader("neutralwords.txt");
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    file2.Close();
                }
                else
                {
                    file2.Close();
                    StreamWriter file = new StreamWriter("neutralwords.txt", true);
                    file.WriteLine(x[yx].ToLower());
                    yx++;
                    file.Close();
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            int s = sadchecker(textBox1.Text);
            int n = neutralchecker(textBox1.Text);
            int h = happychecker(textBox1.Text);
            string toput = "";
            if (s > n & s > h)
            {
                sadadder();
                MessageBox.Show("Sad");
            }
            else if (n > s & n > h)
            {
                neutadder();
                MessageBox.Show("Neutral");
            }
            else if (h > s & h > n)
            {
                happyadder();
                MessageBox.Show("Happy!");
            }
            else
            {
                MessageBox.Show("Hmm... That's a hard one. Could you tell us?");
                toput = textBox1.Text;
            }
            textBox1.Text = toput;
        }
        public int sadchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howsad = 0;
            StreamReader hapsent = new StreamReader("sadsent.txt");
            if (hapsent.ReadToEnd().ToLower().Contains(xy))
            {
                howsad = 1000000;
            }
            hapsent.Close();
            StreamReader file2 = new StreamReader("sadwords.txt", true);
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howsad++;
                }
            }
            file2.Close();
            return howsad;
        }
        public int happychecker(string xy)
        {
            string[] x = xy.Split(' ');
            int yx = 0;
            int howhappy = 0;
            StreamReader file2 = new StreamReader("happywords.txt");
            StreamReader hapsent = new StreamReader("happysent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
                howhappy = 1000000;
            }
            hapsent.Close();
            foreach (string i in x)
            {
                if (file2.ReadToEnd().ToLower().Contains(x[yx]))
                {
                    howhappy++;
                }
            }
            file2.Close();
            return howhappy;
        }
        public int neutralchecker(string xy)
        {
            string[] x = xy.Split(' ');
            int howneutral = 0;
            StreamReader hapsent = new StreamReader("neutsent.txt");
            if (hapsent.ReadToEnd().Contains(xy))
            {
               howneutral = 1000000;
            }
            hapsent.Close();
            int yx = 0;
            StreamReader file2 = new StreamReader("neutralwords.txt");
            foreach (string i in x)
            {
                if (file2.ReadToEnd().Contains(x[yx]))
                {
                    howneutral++;
                }
            }
            file2.Close();
            return howneutral;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show(hapout());
        }
        /// <summary>
        /// returns a string with the non-happy words taken out :)
        /// </summary>
        public string hapout()
        {
            string[] towork = textBox1.Text.Split(' ');
            StreamReader hapword = new StreamReader("happywords.txt");
            StreamReader sadword = new StreamReader("sadwords.txt");
            string hapwords = hapword.ReadToEnd();
            string sadwords = sadword.ReadToEnd();
            hapword.Close();
            sadword.Close();
            string end = "";
            foreach (string i in towork)
            {
                if (sadwords.ToLower().Contains(i))
                {
                    if (hapwords.ToLower().Contains(i))
                    {
                        end = end + i + " ";
                    }
                    else
                    {
                        end = end + "<!ALERT!~sad txt~!ALERT!>" + " ";
                    }
                }
                else
                {
                    end = end + i + " ";
                }
            }
            return end;
        }
        private void button6_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// returns a string with the non-happy words taken out :)
        /// </summary>
        public string sadout()
        {
            string[] towork = textBox1.Text.Split(' ');
            StreamReader sadword = new StreamReader("sadwords.txt");
            StreamReader hapword = new StreamReader("hapwords.txt");
            string hapwords = hapword.ReadToEnd();
            string sadwords = sadword.ReadToEnd();
            hapword.Close();
            sadword.Close();
            string end = "";
            foreach (string i in towork)
            {
                if (hapwords.ToLower().Contains(i))
                {
                    if (sadwords.ToLower().Contains(i))
                    {
                        end = end + i + " ";
                    }
                    else
                    {
                        end = end + "<!ALERT!~happy txt~!ALERT!>" + " ";
                    }
                }
                else
                {
                    end = end + i + " ";
                }
            }
            return end;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            StreamWriter x = new StreamWriter("happywords.txt");
            StreamWriter xy = new StreamWriter("happysent.txt");
            x.Write("");
            xy.Write("");
            x.Close();
            xy.Close();
            MessageBox.Show("CLEARED");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            StreamWriter x = new StreamWriter("neutralwords.txt");
            StreamWriter xy = new StreamWriter("neutsent.txt");
            x.Write("");
            xy.Write("");
            x.Close();
            xy.Close();
            MessageBox.Show("CLEARED");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            StreamWriter x = new StreamWriter("sadwords.txt");
            StreamWriter xy = new StreamWriter("sadsent.txt");
            x.Write("");
            xy.Write("");
            x.Close();
            xy.Close();
            MessageBox.Show("CLEARED");
        }
    }
}
